import { DefaultCompare, ICompareFunction } from "../public/Utils"

export abstract class Sort<T> {
    private _array: Array<T>
    private _compare: ICompareFunction

    /**
     * 排序算法
     * @param array 需要进行排序的数组
     * @param compareFn 比对函数
     */
    constructor(array: Array<T> = [], compare: ICompareFunction = new DefaultCompare()) {
        this._array = array
        this._compare = compare
    }

    public get array(): Array<T> {
        return this._array
    }
    public set array(value: Array<T>) {
        this._array = value
    }

    public get compare(): ICompareFunction {
        return this._compare
    }
    public set compare(value: ICompareFunction) {
        this._compare = value
    }

    public swap(array:Array<T>,item1 : number, item2 : number){
        //通过解构调换元素位置
        [array[item1],array[item2]] = [array[item2],array[item1]]

        //array.splice(item1,1,array[item2])删除item1并返回[array[item1]]
        // array.splice(item2,1,...[array[item1]])删除item2，从而实现交换两个元素
        // array.splice(item2,1,...array.splice(item1,1,array[item2]))
    }

    abstract sort() : void | Array<T>
}

